package com.learn.springlearn.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("redis")
public class RedisController {

    @Resource
    private RedisTemplate<String,Integer> redisTemplate;


    @RequestMapping("/get")
    public String getRedis(){
        Integer increment = redisTemplate.opsForValue().get("increment");
        if (increment == null){
            redisTemplate.opsForValue().set("increment",1,2, TimeUnit.HOURS);
            increment = 1;
        }else {
            redisTemplate.opsForValue().increment("increment");
            increment++;
        }
        return increment.toString();
    }
}
